package com;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;


public class GameContainer extends Observable {
    public static final int ROWS = 30;
    public static final int COLS = 10;
    public static final int SPEED = 200;

    private List<Block[]> data;
    private Shape shape;
    private int scores = 0;
    private String scoresStr;

    public int getScores() {
        return scores;
    }

    public void setScores(int scores) {
        this.scores = scores;
    }


    public String getScoresStr() {
        return scoresStr;
    }

    public void setScoresStr(String scoresStr) {
        this.scoresStr = scoresStr;
    }


    public GameContainer() {
        data = new ArrayList<>();
        for (int i = 0; i < ROWS; i++) {
            Block[]  row = new Block[COLS];
            for (int j = 0; j < row.length; j++) {
                row[j] = new Block();
            }
            data.add(row);
        }
    }

    public void start() {
        shape = ShapeFactory.getShape();
        setChanged();
        notifyObservers(shape);

        //结束游戏
        if (!canDown()) {
            System.out.println("GameOver!!");
            System.exit(0);
        }

        new Thread(new Runnable() { //下降
            @Override
            public void run() {
                while (canDown()) {
                    shape.down();
                    setChanged();
                    notifyObservers(shape);
                    try {
                        Thread.sleep(SPEED);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                saveShape();
                clearRow();
                setChanged();
                notifyObservers(data);
                start();
            }
        }).start();
    }

    private void clearRow() {
        int counter = 0;
        List<Block[]> data=new ArrayList<>();
        int fullRows=0;
        for(int x=0;x<this.data.size();x++){
            Block[] row=this.data.get(x);
            int count=0;
            for(int i=0;i<row.length;i++) {
                if (row[i].value == 1) count++;
            }
            if(count==COLS){
                fullRows++;
            }
            else{
                data.add(row);
                counter++;
            }
        }
        for(int i=0;i<fullRows;i++){
            Block[] tmp=new Block[COLS];
            for(int j=0;j<tmp.length;j++)
                tmp[j]=new Block();
            data.add(i,tmp);
        }
        this.data=data;
        if(fullRows==1){
            scores+=100;
        }
        if (fullRows==2){
            scores+=210;
        }
        if(fullRows==3){
            scores+=400;
        }
        if(fullRows==4) {
            scores+=800;
        }

        scoresStr=String.valueOf(scores);
    }

    private void saveShape() {
        int[][] sd=shape.getData();
        int top=shape.getTop();
        int left=shape.getLeft();
        for(int i=0;i<sd.length;i++){
            Block[] tmp=data.get(top+i);
            for(int j=0;j<sd[i].length;j++){
                if(sd[i][j]==1){
                    tmp[left+j].value=sd[i][j];
                    System.out.println();
                    tmp[left+j].color=shape.getColor();
                }
            }

        }
    }




    public boolean canDown() {
        int top = shape.getTop();
        int left = shape.getLeft();
        int[][] sd = shape.getData();
        //int[][] data=shape.getData();
        for (int i = sd.length - 1; i >= 0; i--) {
            Block[] row = data.get(top + i + 1);
            for (int j = 0; j < sd[i].length; j++) {
                if (sd[i][j] == 1 && (top + i > 26 || row[left + j].value == 1)) {
                    return false;
                }
            }
        }
        return true;
    }


    public boolean canLeft() {
        int top = shape.getTop();
        int left = shape.getLeft();
        int[][] sd = shape.getData();
        for (int i = sd.length - 1; i >= 0; i--) {
            Block[] row = data.get(top + i);
            for (int j = 0; j < sd[i].length; j++) {

                if (sd[i][j] == 1 && (left + j == 0 || row[left + j - 1].value == 1)) {
                    return false;
                }
            }
        }
        return true;
    }

    public boolean canRight() {
        int top = shape.getTop();
        int left = shape.getLeft();
        int[][] sd = shape.getData();
        for (int i = sd.length - 1; i >= 0; i--) {
            Block[] row = data.get(top + i);
            for (int j = 0; j < sd[i].length; j++) {

                if (sd[i][j] == 1 && (left + j == 9 || row[left + j + 1].value == 1)) {
                    return false;
                }
            }
        }
        return true;
    }

    class Block{
        int value;
        Color color;
    }
}
